function [dinamicLossDown, dinamicLossUp, stageLow, stageUp]=dinamicLoss (main, filterType, wa, wp, waMAS, waMENOS, wpMAS, wpMENOS)

switch filterType
    case 2
        bandOI=logspace(0, log10(wp), 1000);
    case 3
        bandOI=logspace(log10(wp), 6, 1000);
    case 1
        bandOI=logspace(log10(wpMENOS), log10(wpMAS), 1000);
    case 4
        bandOI1=logspace(0, log10(wpMENOS), 500);
        bandOI2=logspace(log10(wpMAS), 6, 500);
        for j=1:1000
            if j<=500
                bandOI(j)=bandOI1(j);
            else
                bandOI(j)=bandOI2(j-500);
            end
        end
end

mag=zeros([1, main.stageAmount]);
for i = 1:main.stageAmount
        
    stageID = main.stageTracking{i,1};   
    stage = ['stage' num2str(stageID)];   
    H=main.(stage).stageTransferFunction;

    MAG=bode(H, bandOI);
    MAG=20*log10(MAG);

    for k=1:length(MAG)
    mag(i,k)=MAG(:,:,k);
    end
end

dinamicLossDown=min(mag(1,:));
dinamicLossUp=max(mag(1,:));
    
for i=1:length(mag(:,1))
    stageLow(i)=min(mag(i,:));
    stageUp(i)=max(mag(i,:));
    if i~=1
        mag(1,:)=mag(1,:)+mag(i,:);
    end
    
    low=min(mag(1,:));
    up=max(mag(1,:));

    if low<dinamicLossDown
        dinamicLossDown=low;
    end
    if up>dinamicLossUp
        dinamicLossUp=up;
    end
end
if dinamicLossUp<0
    dinamicLossUp=0;
end
if dinamicLossDown>0
    dinamicLossDown=0;
else dinamicLossDown=-dinamicLossDown;
end

end
